//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
Page({
    data: {
        userInfo: {},
        logged: false,
        takeSession: false,
        requestResult: '',
        addflag:true,
        focus:false,
        list:[
          { title: 'list1', status: 0, id: 1, class: 'bc1' },
          { title: 'list2', status: 0, id: 2, class: 'bc2' },
          { title: 'list3', status: 1, id: 3, class: 'bc3' },
          { title: 'list1', status: 0, id: 1, class: 'bc2' },
          { title: 'list2', status: 0, id: 2, class: 'bc2' },
          { title: 'list3', status: 0, id: 3, class: 'bc1' },
        ],
        value:'',
        history:false,
    },
    showhistory: function () {
      var h = this.data.history
      this.setData({
        history: !h
      })
      console.log(this.data.history)
    },
    cancelcomplete: function () {
      this.checkLogin()
    }, 
    onLoad: function(){
      this.checkLogin()
    }, 
    onPullDownRefresh:function(){
      wx.stopPullDownRefresh();  
      this.setData({
        addflag:false,focus:true
      })
    },
    onReachBottom: function () {
      console.log(2)
      // Do something when page reach bottom.
    },
    new: function (e) {
      var that = this
      if (e.detail.value) {
        var value = { title: e.detail.value }
        var list = that.data.list
        list.unshift(value)
        that.setData({
          list: list, value: '', addflag: true, focus: false
        })
      }else{
        that.setData({
          addflag: true, focus: false
        })
      }
      // Do something when page reach bottom.
    },
    save:function(v){
      console.log(v,11)
    },
    complete:function(e){
      if(e.detail.value){
        console.log(e.detail)
      }
      console.log(e)
    },
    checkLogin:function(){
      var that = this
      wx.getStorage({
        key: "userInfo",
        success: function (res) {
          if (res.data) {
            that.setData({
              userInfo: JSON.parse(res.data),
              logged: true
            })
          } else {
            that.login()
          }
        },
        fail: function (res) {
          that.login()
        }
      })
    },
    // 用户登录示例
    login: function() {
        if (this.data.logged) return
        util.showBusy('正在登录')
        var that = this
        // wx.getUserInfo({
        //   success: function (res) {
        //     that.setData({
        //         userInfo: res.userInfo,
        //         logged: true
        //     })
        //     wx.setStorage({
        //       key: "userInfo",
        //       data: JSON.stringify(res.userInfo)
        //     })
        //     util.showSuccess('登录成功')
        //   }
        // })
        // 调用登录接口
        qcloud.login({
            success(result) {
                if (result) {
                    util.showSuccess('登录成功')
                    that.setData({
                        userInfo: result,
                        logged: true
                    })
                } else {
                    // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
                    qcloud.request({
                        url: config.service.requestUrl,
                        login: true,
                        success(result) {
                            util.showSuccess('登录成功')
                            that.setData({
                                userInfo: result.data.data,
                                logged: true
                            })
                            wx.setStorage({
                              key: "userInfo",
                              data: JSON.stringify(result.data.data)
                            })
                        },
                        fail(error) {
                            util.showModel('请求失败', error)
                            console.log('request fail', error)
                        }
                    })
                }
            },
            fail(error) {
                util.showModel('登录失败', error)
                console.log('登录失败', error)
            }
        })
    }
})
